package com.i2works.smartluolong.utils.common;

import org.apache.shiro.codec.Base64;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;

/**
 * @author Liu Yutao
 * @className ShiroCipher
 * @email koal@vip.qq.com
 * @date 2016/11/27 1:10
 */

public class ShiroCipher {


    /**
     * AES生成器
     */
    public static String KeyGenerator() {
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecretKey deskey = keygen.generateKey();
            return Base64.encodeToString(deskey.getEncoded());
        } catch (NoSuchAlgorithmException e) {
            System.err.println("生成shiro的rememberCookie加密密钥异常：" + e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            System.err.println(KeyGenerator());
        }
    }
    /**
     * 加解密测试
     */
    public static void shiroCipherDemo() {
        System.err.println(KeyGenerator());
//        Preconditions.checkArgument(!Strings.isNullOrEmpty("UjOIxaMfxfMivz38bo4gZ2CPvSDhMdO8h42spLkCzdY="), "消息摘要不能为空");
//        System.out.println(Base64.decodeToString("UjOIxaMfxfMivz38bo4gZ2CPvSDhMdO8h42spLkCzdY="));

        //shiro base64编码/解码操作
//        String str = "hello";
//        String base64Encoded = Base64.encodeToString(str.getBytes());
//        String str2 = Base64.decodeToString(base64Encoded);
//        System.out.println(base64Encoded);
//        System.out.println(str2);
        //shiro 16进制字符串编码/解码操作
//        String str = "hello";
//        String base64Encoded = Hex.encodeToString(str.getBytes());
//        String str2 = new String(Hex.decode(base64Encoded.getBytes()));
//        System.out.println(base64Encoded);
//        System.out.println(str2);

//        String str = "hello";
//        String salt = "123";
//        String md5 = new Md5Hash(str, salt).toString();
//        System.out.println(md5);

//        String str = "hello";
//        String salt = "123";
//        //内部使用MessageDigest
//        String simpleHash = new SimpleHash("SHA-1", str, salt).toString();
//        System.out.println(simpleHash);

//        //SecureRandomNumberGenerator用于生成一个随机数
//        SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
//        randomNumberGenerator.setSeed("123".getBytes());
//        String hex = randomNumberGenerator.nextBytes().toHex();
//        System.out.println(hex);
////        AES算法实现
//        AesCipherService aesCipherService = new AesCipherService();
//        aesCipherService.setKeySize(128); //设置key长度
//        //生成key
//        Key key = aesCipherService.generateNewKey();
//        String text = "hello";
//        //加密
//        String encrptText = aesCipherService.encrypt(text.getBytes(), key.getEncoded()).toHex();
//        System.out.println("加密:"+encrptText);
//        //解密
//        String text2 =  new String(aesCipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());
//        System.out.println("解密:"+text2);
    }
}
